package com.ycfmedical.bed.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.bBcApplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 使用sharepreference存取数据
 * Created by Egoist on 2017/1/6.
 */
public class ShareUtil {

    private Context context;

    private static class holder{
        static ShareUtil sSingleton = new ShareUtil(bBcApplication.getContext());
    }

    public static ShareUtil getInstance() {
        return holder.sSingleton;
    }


    private ShareUtil(Context context) {
        this.context = context;
    }

    /**
     * 保存参数
     *
     * @throws Exception
     */

    public void saveInt(String key, Integer value) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Editor editor = preference.edit();

        editor.putInt(key, value);

        editor.commit();// 把数据提交会文件

    }

    /**
     * 保存参数
     *
     * @throws Exception
     */

    public void saveLong(String key, Long value) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Editor editor = preference.edit();

        editor.putLong(key, value);

        editor.commit();// 把数据提交会文件

    }


    /**
     * 保存参数
     *
     * @param name
     * @throws Exception
     */

    public void saveString(String name, String valeu) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Editor editor = preference.edit();

        editor.putString(name, valeu);
        editor.commit();// 把数据提交会文件

    }


    /**
     * 保存参数
     *
     * @throws Exception
     */

    public void saveBoolean(String key, Boolean value) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Editor editor = preference.edit();

        editor.putBoolean(key, value);

        editor.commit();// 把数据提交会文件

    }

    /**
     * 获取各项配置参数
     *
     * @return
     */

    public String getString(String key) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        String value = preference.getString(key, "");

        return value;

    }


    /**
     * 获取各项配置参数
     *
     * @return
     */

    public int getInt(String key) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        int value = preference.getInt(key, 0);

        return value;

    }

    /**
     * 获取各项配置参数
     *
     * @return
     */

    public Long getLong(String key) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Long value = preference.getLong(key, 0L);

        return value;

    }


    /**
     * 获取各项配置参数
     *
     * @return
     */

    public boolean getBoolean(String key) {

        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        boolean value = preference.getBoolean(key, false);

        return value;

    }

    /**
     * 针对复杂类型存储<对象>
     */
    public void setObject(String key, Object object) {
        SharedPreferences sp = this.context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME, Context.MODE_PRIVATE);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {

            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray()));
            Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T getObject(String key, Class<T> clazz) {
        SharedPreferences sp = this.context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, null);
            byte[] buffer = Base64.decode(objectVal);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    /**
     * 根据key删除值
     *
     * @param key
     */
    public void deleteConfig(String key) {
        SharedPreferences preference = context.getSharedPreferences(Constants.SHARE_PREF_XMLNAME,
                Context.MODE_PRIVATE);

        Editor editor = preference.edit();

        editor.remove(key);

        editor.commit();
    }

}
